package com.example.zz.example;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zz.example.network.bean.MainBean;

import java.util.ArrayList;
import java.util.List;

class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.MyHolder> {

    private ArrayList<MainBean> mainBeansList = new ArrayList<>();
    private OnItemClickListener listener;

    public MainRecyclerViewAdapter() {

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;

    }

    public void updateData(ArrayList<MainBean> mainBeansList) {
        this.mainBeansList = mainBeansList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = View.inflate(parent.getContext(), R.layout.main_recycler_view_item, null);
        MyHolder myHolder = new MyHolder(inflate);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.setData(mainBeansList.get(position), position);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);

//        if (payloads.isEmpty()) {
        onBindViewHolder(holder, position);
//        } else {
//            holder.setSpecialData(payloads.get(0).toString());
//        }

    }

    @Override
    public int getItemCount() {
        return mainBeansList.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        private Context mContext;
        private TextView recyclerTitle;
        private ImageView imageView;
        private TextView passtimeTitle;
        private MainBean mainBean;
        private int mPosition;


        public MyHolder(View itemView) {
            super(itemView);
            this.mContext = itemView.getContext();
            recyclerTitle = itemView.findViewById(R.id.title_recycler);
            imageView = itemView.findViewById(R.id.image_recycler);

            imageView.setOnClickListener(view -> {
                listener.onItemClick(mPosition);
            });
        }

        public void setData(MainBean mainBean, int position) {
            this.mainBean = mainBean;
            this.mPosition = position;
            if (position % 4 == 0) {
                imageView.setImageResource(R.drawable.btn_main_selector5);
            } else if (position % 2 == 0) {
                imageView.setImageResource(R.drawable.btn_main_selector4);
            } else if (position % 3 == 0) {
                imageView.setImageResource(R.drawable.btn_main_selector3);
            } else {
                imageView.setImageResource(R.drawable.btn_main_selector2);
            }
            recyclerTitle.setText(mainBean.getName());
        }

        public void setSpecialData(String title) {
            recyclerTitle.setText(title);
        }
    }

    public interface OnItemClickListener {
        public void onItemClick(int position);
    }
}
